package de.tsystems.jsfdict.tools;

import de.tsystems.jsfdict.api.LanguageService;
import de.tsystems.jsfdict.api.WordService;
import de.tsystems.jsfdict.model.Language;
import de.tsystems.jsfdict.model.Word;
import org.apache.commons.lang.StringUtils;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.inject.Inject;
import javax.inject.Named;

@Named("wordConverter")
public class WordConverter implements Converter {

    @Inject
    @Named("wordService")
    private WordService wordService;

    public WordService getWordService() {
        return wordService;
    }

    public void setWordService(WordService wordService) {
        this.wordService = wordService;
    }

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        if (StringUtils.isEmpty(value)) {
            return null;
        }

        return wordService.getById(value);
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        return ((Word) value).getId();
    }
}
